﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Kit3D.Windows.Media;
using Kit3D.Windows.Media.Media3D;

namespace TestApp
{
    public partial class CelestialBodiesDeclarative : UserControl
    {
        public CelestialBodiesDeclarative()
        {
            // Required to initialize variables
            InitializeComponent();
            var obj = theSunModel;
        }

        private bool refsSet;

        protected RotateTransform3D sunRotate;
        protected RotateTransform3D earthMoonRotate;
        protected RotateTransform3D earthRotate;
        protected RotateTransform3D moonRotate;

        /// <summary>
        /// Silverlight's XamlLoader will set the Name property on FrameworkElements from the x:Name attribute in the XAML,
        /// and the designer-generated .g.cs file will write the fields into the class, but its (and ours, for that matter)
        /// FindName calls won't find them on anything other than the highest-level non-native element in the XAML.  Need to
        /// write a FindNameEx that all Kit3D elements will inherit, to walk the object graph, named FrameworkElements, so
        /// these null fields can be assigned properly.
        /// </summary>
        private void SetReferences()
        {
            var sunModel = (GeometryModel3D)((ModelVisual3D)viewport.Children[0]).Content;
            var sunTransformGroup = (Transform3DGroup)sunModel.Transform;
            
            sunRotate = (RotateTransform3D)sunTransformGroup.Children[1];

            var earthMoonModel = (ModelVisual3D)viewport.Children[1];
            var earthMoonTransformGroup = (Transform3DGroup)earthMoonModel.Transform;
            
            earthMoonRotate = (RotateTransform3D)earthMoonTransformGroup.Children[1];
            
            var earthMoonModelGroup = (Model3DGroup)earthMoonModel.Content;
            var earthModel = (GeometryModel3D)earthMoonModelGroup.Children[0];
            
            earthRotate = (RotateTransform3D)earthModel.Transform;
            
            var moonModel = (GeometryModel3D)earthMoonModelGroup.Children[1];
            var moonRotateTransformGroup = (Transform3DGroup)moonModel.Transform;
            
            moonRotate = (RotateTransform3D)moonRotateTransformGroup.Children[2];

            refsSet = true;
        }

        private void AnimationLoop_Completed(object sender, EventArgs e)
        {
            if (!refsSet)
                SetReferences();

            ((AxisAngleRotation3D)sunRotate.Rotation).Angle += 1;
            ((AxisAngleRotation3D)earthMoonRotate.Rotation).Angle += 0.2;
            ((AxisAngleRotation3D) earthRotate.Rotation).Angle += 3;
            ((AxisAngleRotation3D)moonRotate.Rotation).Angle += 5;

            this.AnimationLoop.Begin();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.AnimationLoop.Begin();
        }
    }
}
